<template>
  <div class="bindingPhone">
    <top-bar :title="'绑定手机'"/>
    <div class="content">
      <div class="instruction">{{ instruction }}</div>
      <div class="content-bottom phone">
        <el-input v-model="phone" type="number" placeholder="请输入手机号码"></el-input>
      </div>
      <div class="content-bottom">
        <el-input v-model="input" placeholder="请输入验证码"></el-input>
        <span @click="getCode">{{ gain }}</span>
      </div>
    </div>
    <div class="next" @click="checkold">
      <button>确定</button>
    </div>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'
import topBar from './topBar'
import * as api from '../../api'

export default {
  name: 'bindingPhone',
  components: {
    topBar
  },
  data () {
    return {
      input: '',
      instruction: '目前仅支持中国大陆手机号码，绑定后可直接做为账号登录',
      phone: '',
      gain: '获取验证码',
      sendStatus: true,
    }
  },
  computed: {
    actionStatus () {
      return this.$store.state.actionStatus
    },
    ...mapGetters(['succMsg', 'failMsg'])
  },
  watch: {
    actionStatus () {
      if (!this.actionStatus) return false
      this.smsCd()
    },
    succMsg () {
      this.succMsg && this.Toast(this.succMsg, 500)
    },
    failMsg () {
      this.failMsg && this.Toast(this.failMsg, 500)
    }
  },
  methods: {
    // 发送验证码
    getCode () {
      let phoneNums = /^1\d{10}$/
      if (this.phone == '') {
        this.Toast('请输入手机号！', 500)
        return false
      } else if (!phoneNums.test(this.phone)) {
        this.Toast('请输入合法的手机号！', 500)
        return false
      } else {
        this.$store.dispatch('fSmsSend', {
          'sms-type': 6,
          'sms-mobile': this.phone
        })
      }
    },
    // 确定
    checkold () {
      let input = this.input.trim()
      if (!this.phone) {
        this.Toast('请输入手机号！', 500)
        return false
      }
      if (!input) {
        this.Toast('请输入验证码！', 500)
        return false
      }
      let obj = {
        'sms-type': 4,
        'sms-code': input,
        'sms-mobile': this.phone
      }
      api.bindMobile(obj, succ => {
        this.$store.dispatch('getUserDetail')
        this.$router.go(-2)
        this.Toast('绑定成功', 500)
      }, err => {
        this.Toast(err, 500)
      })
    },
    // 发送验证码
    smsCd () {
      if (!this.sendStatus) {
        return
      }
      this.sendStatus = false
      let t = 60
      this._t = window.setInterval(() => {
        t--
        if (t === 0) {
          clearInterval(this._t)
          this.sendStatus = true
          this.$store.state.actionStates = false
          this.gain = '获取验证码'
        } else {
          this.gain = t + '秒后重试'
        }
      }, 1000)
    },
  }
}
</script>

<style lang="scss" scoped>
.bindingPhone {
  padding: 0.75rem 0.48rem;

  .content {
    .content-bottom {
      width: 100%;
      height: 1.39rem;
      background: rgba(255, 255, 255, 0.6);
      border-radius: 0.21rem;
      display: flex;
      align-items: center;

      /deep/ .el-input__inner {
        height: 1.39rem;
        border: none;
        padding-left: 0.4rem;
        border-radius: 0.21rem 0 0 0.21rem;
        font-size: 0.32rem;
      }

      > span {
        width: 2.22rem;
        font-size: 0.29rem;
        font-family: SourceHanSansCN-Regular, SourceHanSansCN;
        font-weight: 400;
        color: #FF1D1D;
        margin-right: 0.67rem;
      }
    }

    .instruction {
      width: 100%;
      height: 1.39rem;
      line-height: 1.39rem;
      font-size: 0.32rem;
      font-family: SourceHanSansCN-Regular, SourceHanSansCN;
      font-weight: 400;
      color: #363739;
      text-align: center;
    }

    .phone {
      margin-bottom: 0.37rem;

      /deep/ .el-input__inner {
        border-radius: 0.21rem;
      }
    }
  }

  .next {
    padding: 0 0.45rem;

    button {
      margin-top: 0.96rem;
      width: 8.13rem;
      height: 1.2rem;
      background: #FF751A linear-gradient(90deg, #FF0000 0%, #FFD500 100%);
      border-radius: 0.67rem;
      font-size: 0.45rem;
      font-family: SourceHanSansCN-Medium, SourceHanSansCN;
      font-weight: 500;
      color: #FFFFFF;
    }
  }
}
</style>
